package com.place;

import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.faces.context.FacesContext;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class google {

	public ArrayList places = new ArrayList();
	public ArrayList deals = new ArrayList();
	public String keyword;
	public String zipcode;
	public String type;
	public String lat;
	public String getLat() {
		this.lat = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("lat");
		return lat;
	}

	public void setLat(String lat) {
		this.lat = lat;
	}

	public String getLog() {
		
		this.log = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("log"); 
		return log;
	}

	public void setLog(String log) {
		this.log = log;
	}

	public String log;

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setPlaces(ArrayList places) {
		this.places = places;
	}

	public ArrayList getDeals() {
		return deals;
	}

	public void setDeals(ArrayList deals) {
		this.deals = deals;
	}

	public ArrayList getPlaces() {
		return places;
	}

	public void search() {
		deals.clear();
		deals.addAll(Deals.getdeals(zipcode));
		places.clear();
		URL oracle;
		try {
			String log_lat = com.place.zipcode.getll(zipcode);
			oracle = new URL(
					"https://maps.googleapis.com/maps/api/place/search/xml?location="
							+ log_lat
							+ "&radius=5000&types="
							+ type
							+ "&name="
							+ keyword
							+ "&sensor=false&key=AIzaSyBb6vmvc_7HzHLfi7Iq7q5HBn8Ynr55wmA");

			URLConnection yc = oracle.openConnection();
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(yc.getInputStream());

			NodeList listOfPersons = doc.getElementsByTagName("result");
			int totalPersons = listOfPersons.getLength();
			System.out.println("Total no of results : " + totalPersons);
			for (int s = 0; s < listOfPersons.getLength(); s++) {

				Place place = new Place();
				Node firstPersonNode = listOfPersons.item(s);
				if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {

					Element firstPersonElement = (Element) firstPersonNode;

					// -------
					NodeList firstNameList = firstPersonElement
							.getElementsByTagName("name");
					Element firstNameElement = (Element) firstNameList.item(0);

					NodeList textFNList = firstNameElement.getChildNodes();
					place.setName(((Node) textFNList.item(0)).getNodeValue()
							.trim());
					System.out.println("Name : " + place.getName());

					// -------
					NodeList lastNameList = firstPersonElement
							.getElementsByTagName("vicinity");
					Element lastNameElement = (Element) lastNameList.item(0);

					NodeList textLNList = lastNameElement.getChildNodes();
					place.setAddress(((Node) textLNList.item(0)).getNodeValue()
							.trim());
					System.out.println("Address : " + place.getAddress());

					// ----
					NodeList ageList = firstPersonElement
							.getElementsByTagName("rating");
					Element ageElement = (Element) ageList.item(0);
					if (ageElement != null) {

						NodeList textAgeList = ageElement.getChildNodes();
						place.setRating(((Node) textAgeList.item(0))
								.getNodeValue().trim());
						System.out.println("Rating : " + place.getRating());
					}
					NodeList ageList1 = firstPersonElement
							.getElementsByTagName("lat");
					Element ageElement1 = (Element) ageList1.item(0);
					
					NodeList ageList2 = firstPersonElement
							.getElementsByTagName("lng");
					Element ageElement2 = (Element) ageList2.item(0);
					

						NodeList textAgeList1 = ageElement1.getChildNodes();
						NodeList textAgeList2 = ageElement2.getChildNodes();
						place.setLat(((Node) textAgeList1.item(0))
								.getNodeValue().trim());
								
						place.setLog(((Node) textAgeList2.item(0))
								.getNodeValue().trim());
						
					
					places.add(place);
				}
				System.out.println(places.size());
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void assign()
	{
		this.lat = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("lat");
		this.log = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("log"); 
	}

}
